package com.gc.handler;

import com.gc.common.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    //定义成员变量

    //事件处理器上下文对象 (存储handler信息  写操作)
    private ChannelHandlerContext context;

    //记录服务器返回的数据
    private String result;

    //记录发送给服务器的数据
    private RpcRequest rpcRequest;
    //private String param;

    /**
     * 先出数据,开始唤醒线程
     * @return
     * @throws Exception
     */
    @Override
    public synchronized Object call() throws Exception {
       context.writeAndFlush(rpcRequest);
       wait();
       return  result;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.context=ctx;
    }

    /**
     * 收到服务器数据,唤醒等待线程
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        this.result=msg.toString();
        notify();
    }

 /*   public void setPara(String param) {
        this.param = param;
    }*/

    public void setRpcRequest(RpcRequest rpcRequest) {
        this.rpcRequest = rpcRequest;
    }
}
